//
//  PopoverCell.swift
//  ZZUI
//
//  Created by 张文晏 on 2018/11/9.
//  Copyright © 2018 张文晏. All rights reserved.
//

import UIKit
import SnapKit

class PopoverCell: UITableViewCell {
    
    // MARK: - Subviews
    lazy var actionImageView: UIImageView = {
        let imv = UIImageView()
        return imv
    }()
    lazy var actionTitleLabel: UILabel = {
        let label = UILabel()
        return label
    }()
    lazy var bottomLine: UIView = {
        let line = UIView()
        line.backgroundColor = .init(hex: 0xeeeeee)
        return line
    }()
    
    
    // MARK: - Table view cell
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        contentView.backgroundColor = .clear
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func set(image: UIImage?, text: String, font: UIFont, textColor: UIColor, isShowLine: Bool) {
        contentView.subviews.forEach { $0.removeFromSuperview() }
        if let actionImage = image {
            contentView.addSubview(actionImageView)
            actionImageView.snp.makeConstraints { make in
                make.leading.equalTo(20)
                make.centerY.equalToSuperview()
                make.width.height.equalTo(16)
            }
            actionImageView.contentMode = .scaleAspectFit
            actionImageView.image = actionImage
        }
        contentView.addSubview(actionTitleLabel)
        actionTitleLabel.snp.makeConstraints { make in
            if image != nil {
                make.leading.equalTo(actionImageView.snp.trailing).offset(10)
            } else {
                make.leading.equalTo(20)
            }
            make.trailing.equalTo(-24)
            make.centerY.equalToSuperview()
        }
        actionTitleLabel.font = font
        actionTitleLabel.textColor = textColor
        actionTitleLabel.text = text
        if isShowLine {
            contentView.addSubview(bottomLine)
            bottomLine.snp.makeConstraints { make in
                make.leading.equalTo(8)
                make.bottom.trailing.equalToSuperview()
                make.height.equalTo(1)
            }
        }
    }}
